package com.dz.lm.web.controller.newuser.userorder;

import com.alibaba.fastjson.JSONObject;
import com.dz.lm.common.exception.ServiceException;
import com.dz.lm.common.util.StringUtil;
import com.dz.lm.common.utils.json.JsonUtils;
import com.dz.lm.service.userorder.UserOrderService;
import com.dz.lm.web.controller.BasicControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;

/**
 * @version 1.8;
 * @author: LDY;
 * @Description;
 * @Date: Created in 2020/7/18 14:30;
 */
@RestController
@RequestMapping(value = "/h5/weijie/rewriteUrl/userOrder", produces = "application/json")
public class UserOrderController extends BasicControl {

    @Autowired
    private UserOrderService userOrderService;
    /**
     * 列表
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/list")
    public Map list(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData);
        //商家
        Integer businessId = param.get("businessId") != null ? Integer.parseInt(param.get("businessId").toString()) : 0;
        Integer pageNum = param.get("page") != null ? Integer.parseInt(param.get("page").toString()) : 1;
        Integer rows = param.get("rows") != null ? Integer.parseInt(param.get("rows").toString()) : 10;
        data = userOrderService.query(pageNum + 1, rows, businessId);
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
    /**
     * 商家成交信息
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/dealInfo")
    public Map dealInfo(HttpServletRequest request) throws ServiceException {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData);
        //商家
        Integer businessId = param.get("businessId") != null ? Integer.parseInt(param.get("businessId").toString()) : 0;
        data = userOrderService.dealInfo( businessId);
        message = "获取成功";
        result = "00";
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
    /**
     * 处理订单
     *
     * @param request
     * @return
     * @throws ServiceException
     */
    @ResponseBody
    @RequestMapping("/handle")
    public Map handle(HttpServletRequest request) throws Exception {
        String result = "01";
        String message = "";
        Object data = null;
        JSONObject subjo = new JSONObject();
        String reqData = request.getAttribute("_reqData") != null ? request.getAttribute("_reqData").toString() : "";
        Map<String, Object> param = JsonUtils.readValue(reqData.toString());
        String uId = request.getAttribute("userId").toString();
        //参数校验
        Vector<String> inparam = new Vector<String>();

        for (String in : inparam) {
            if (!param.containsKey(in)) {
                subjo = new JSONObject();
                subjo.put("result", "01");
                subjo.put("message", "参数" + in + "异常");
                return subjo;
            }
        }
        //
        String orderId = param.get("orderId") != null ? param.get("orderId").toString() : "";
        //0已付款 1代发货 2待确认 3 交易成功4 申请退款 5同意退款6 拒绝退款 -1 未支付
        Integer status = param.get("status") != null ? StringUtil.getInt(param.get("status"),-1) :-1;
        //物流号
        String loginsticId = param.get("loginsticId") != null ? param.get("loginsticId").toString() : "";
        //评论
        String remark = param.get("remark") != null ? param.get("remark").toString() : "";

        int update = userOrderService.update(orderId, 0, status, loginsticId, remark, StringUtil.getInt(uId, 0));
        if(update > 0){
            result ="00";
            message="操作成功";
        }
        if (param.get("platform").equals("Android")) {
            Map<String, Object> retData = new HashMap();
            retData.put("message", message);
            retData.put("result", result);
            retData.put("data", data);
            subjo.putAll(retData);
            return parameterEncrypt(request, subjo);
        } else {
            subjo.put("message", message);
            subjo.put("result", result);
            subjo.put("data", data);
            return subjo;
        }
    }
}
